﻿using BCCommon;
using BCEntity.OA.BusinessFlowModel.BusinessFlowStepModel;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.OA.BusinessFlowModel.BusinessFlowStepModel
{
    /// <summary>
    /// OA系统业务流步骤模型
    /// </summary>
	public class BusinessFlowStepModelData : IBusinessFlowStepModelData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_BusinessFlowStepModel";
        private const string QUERY = @"SELECT BusinessFlowStepModelId,BusinessFlowModelId,BusinessFlowModelName,BusinessFlowModelCode,BusinessFlowStepModelName,BusinessFlowStepModelCode,Step,
                                            PageBusinessConfigId,PageBusinessCode,PageBusinessName,CreateTime 
                                        FROM OA_BusinessFlowStepModel";
        public BusinessFlowStepModelData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long businessFlowStepModelId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowStepModelId=?BusinessFlowStepModelId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepModelId", businessFlowStepModelId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(string businessFlowStepModelCode)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowStepModelCode=?BusinessFlowStepModelCode", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepModelCode", businessFlowStepModelCode, DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        public async Task<bool> Exists(long businessFlowModelId, int step)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowModelId=?BusinessFlowModelId AND Step=?Step", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowModelId", businessFlowModelId, DbType.Int64);
            parameters.Add("?Step", step, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> InUsed(long businessFlowModelId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowModelId=?BusinessFlowModelId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowModelId", businessFlowModelId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<BusinessFlowStepModelEntity> Add(BusinessFlowStepModelEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_BusinessFlowStepModel", new string[] { "BusinessFlowModelId", "BusinessFlowModelName", "BusinessFlowModelCode", "BusinessFlowStepModelName",
                   "BusinessFlowStepModelCode", "Step", "PageBusinessConfigId", "PageBusinessCode", "PageBusinessName" });
            sql = string.Format("{0} {1} WHERE BusinessFlowStepModelId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowModelId", entity.BusinessFlowModelId, DbType.Int64, size: 20);
            parameters.Add("?BusinessFlowModelName", entity.BusinessFlowModelName, DbType.AnsiString, size: 20);
            parameters.Add("?BusinessFlowModelCode", entity.BusinessFlowModelCode, DbType.AnsiString, size: 50);
            parameters.Add("?BusinessFlowStepModelName", entity.BusinessFlowStepModelName, DbType.AnsiString, size: 20);
            parameters.Add("?BusinessFlowStepModelCode", entity.BusinessFlowStepModelCode, DbType.AnsiString, size: 50);
            parameters.Add("?Step", entity.Step, DbType.Int32, size: 11);
            parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64, size: 20);
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode, DbType.AnsiString, size: 150);
            parameters.Add("?PageBusinessName", entity.PageBusinessName, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<BusinessFlowStepModelEntity>(sql, parameters, databaseContext.Transaction);

        }

        public async Task<BusinessFlowStepModelEntity> Update(BusinessFlowStepModelEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_BusinessFlowStepModel", new string[] { "BusinessFlowModelId", "BusinessFlowModelName", "BusinessFlowModelCode", "BusinessFlowStepModelName",
                 "BusinessFlowStepModelCode", "Step", "PageBusinessConfigId", "PageBusinessCode", "PageBusinessName" }, new string[] { "BusinessFlowStepModelId" });
            sql = string.Format("{0} {1} WHERE BusinessFlowStepModelId=?BusinessFlowStepModelId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowModelId", entity.BusinessFlowModelId, DbType.Int64, size: 20);
            parameters.Add("?BusinessFlowModelName", entity.BusinessFlowModelName, DbType.AnsiString, size: 20);
            parameters.Add("?BusinessFlowModelCode", entity.BusinessFlowModelCode, DbType.AnsiString, size: 50);
            parameters.Add("?BusinessFlowStepModelId", entity.BusinessFlowStepModelId, DbType.Int64, size: 20);
            parameters.Add("?BusinessFlowStepModelName", entity.BusinessFlowStepModelName, DbType.AnsiString, size: 20);
            parameters.Add("?BusinessFlowStepModelCode", entity.BusinessFlowStepModelCode, DbType.AnsiString, size: 50);
            parameters.Add("?Step", entity.Step, DbType.Int32, size: 11);
            parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64, size: 20);
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode, DbType.AnsiString, size: 150);
            parameters.Add("?PageBusinessName", entity.PageBusinessName, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<BusinessFlowStepModelEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long businessFlowStepModelId)
        {
            string sql = @"DELETE FROM OA_BusinessFlowStepModel WHERE BusinessFlowStepModelId=?BusinessFlowStepModelId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepModelId", businessFlowStepModelId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<BusinessFlowStepModelEntity> GetEntity(long businessFlowStepModelId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowStepModelId=?BusinessFlowStepModelId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepModelId", businessFlowStepModelId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<BusinessFlowStepModelEntity>(sql, parameters);
        }

        public async Task<IEnumerable<BusinessFlowStepModelEntity>> GetEntityByPageBusinessConfigId(long pageBusinessConfigId)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessConfigId=?PageBusinessConfigId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<BusinessFlowStepModelEntity>(sql, parameters);
        }

        public async Task<IEnumerable<BusinessFlowStepModelEntity>> GetList(long businessFlowModelId)
        {
            string sql = string.Format("{0} WHERE BusinessFlowModelId=?BusinessFlowModelId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowModelId", businessFlowModelId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<BusinessFlowStepModelEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<BusinessFlowStepModelEntity>, int)> Query(long? businessFlowModelId,string businessFlowStepModelName,string businessFlowModelName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (businessFlowModelId.HasValue)
            {
                wheres.Add("BusinessFlowModelId=?BusinessFlowModelId");
                parameters.Add("?BusinessFlowModelId", businessFlowModelId, DbType.Int64, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(businessFlowStepModelName))
            {
                wheres.Add("BusinessFlowStepModelName  LIKE CONCAT('%',CONCAT(?BusinessFlowStepModelName,'%'))");
                parameters.Add("?BusinessFlowStepModelName", businessFlowStepModelName, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(businessFlowModelName))
            {
                wheres.Add("BusinessFlowModelName  LIKE CONCAT('%',CONCAT(?BusinessFlowModelName,'%'))");
                parameters.Add("?BusinessFlowModelName", businessFlowModelName, DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_BusinessFlowStepModel {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<BusinessFlowStepModelEntity>(sql, parameters);
        }
    }
}
